1354B - Ternary String - CodeForces Solution


binary search dp implementation two pointers *1200

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
	s = input()
	m = 0
	l = [-1]*3
	for i in range(len(s)):
		l[int(s[i])-1] = i
		if l[0] > -1 and l[1]>-1 and l[2]>-1:
			if m == 0:
				m = max(l)-min(l)+1
			else:
				m = min(m, max(l)-min(l)+1)
	print(m)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define bback(x) x.rbegin(),x.rend()
const int mod = 1e9+7;

int gcd(int a,int b){if(a<b)return gcd(b,a);else if(b==0)return a;else return gcd(b,a%b);}
int isPrime(int n){int p=(int)sqrt(n);for(int i=2;i<=p;i++) if(n%i==0) return 0;return 1;}// reuturn 1 if prime
int ceil(long double a){int b=a;if(a==b){return b;}else{return b+1;}}
int floor(long double a){int b=a;return b;}
int lcm(int a,int b){ return (a*b)/gcd(a,b);}

bool chk_balanced_parenthesis(string exp)
{
	bool flag = true;
	int count = 0;int n = exp.length();
	for (int i = 0; i < n; i++) {
		if (exp[i] == '(') {count++;}
		else {count--;}
		if (count < 0) {flag = false;break;}
	}
	if (count != 0) {flag = false;}
	return flag;
}

bool IsPalindrome(const string& str){
  if (str.empty())
    return false;
  int i = 0, j = str.length() - 1; 
  while (i < j){
    if (str[i] != str[j]){
      return false;
    }
    i++; j--;
  }
  return true;
}

string addBinary(string a, string b) {
  string ans="";
  int i=a.length()-1,j=b.length()-1,carry=0;
  while(i>=0||j>=0||carry){
      if(i>=0){carry+=a[i]-'0'; i--; }
      if(j>=0){carry+=b[j]-'0'; j--; }
      // cout<<carry<<" ";
      ans+=(carry%2+'0');
      cout<<ans<<" ";
      // carry=carry/2;
      // cout<<carry<<endl;
  }
  reverse(ans.begin(),ans.end());
  return ans;
}

int array_gcd(vector<int>& nums) {
  int mn = nums[0], mx = nums[0];
  for(auto n: nums){if(n > mx) mx = n;if(n < mn) mn = n;}
  for(int i = mn; i >= 1; i--){if((mn % i == 0) && (mx % i == 0)) return i;}
  return 1;
}

int and_range(int m, int n) {
  int i = 0;
  while(m != n){
    m >>= 1; n >>= 1; i++;
  }
  return m << i;
}

/********************************************************************************************************************************************/

void solve(){
  string a;
  cin >> a;
  int n=a.size(),ans = INT_MAX;
  // bool ans = false;
  for(int i=1;i<a.size()-1;i++){
    int j=i;
    while(a[j+1]==a[i]&&j+1<n){
      j++;
    }
    if(j+1<n&&i<n&&a[i-1]!=a[i]&&a[i]!=a[j+1]&&a[j+1]!=a[i-1]){
      ans=min(ans,j-i+3);
    }
    i=j;
  }
  if(ans==INT_MAX){
    cout << 0 << endl;
  } 
  else{
    cout << ans << endl;
  }
}

/********************************************************************************************************************************************/

signed main(){
  ios_base::sync_with_stdio(!cin.tie(nullptr));

  int t;
  cin>>t;
  while(t--){
    solve();
  }
}

// int mn=*min_element(a.begin(), a.end());
// vector<int>a(n);
// for(int i=0;i<n;i++){
//   cin>>a[i];
// }

// map<int,int>mp;
// for(int i=0;i<n;i++){
//   int x;
//   cin>>x;
//   mp[x]++;
// }

// if(ans){
//   cout<<"NO"<<endl;
// }
// else{
//   cout<<"YES"<<endl;
// }


Comments

Submit
0 Comments
More Questions

27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome